package com.situ.stmall.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.situ.stmall.admin.entity.APIResult;
import com.situ.stmall.admin.entity.Goods;
import com.situ.stmall.admin.entity.GoodsSort;
import com.situ.stmall.admin.service.GoodsService;

@RestController
@RequestMapping("/api/goods")
@CrossOrigin(origins = {"*"})
public class GoodsController {
	
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 
	 * 添加方法
	 */
	@PostMapping
	public APIResult add(@RequestBody Goods goods) {
		
		goods = goodsService.add(goods);
		
		return APIResult.created(goods);
	}
	
	
	/**
	 * 修改，传参为商品
	 * @param goods
	 * @return
	 */
	@PutMapping
	public APIResult edit(@RequestBody Goods goods) {
		goods = goodsService.edit(goods);
		if (goods != null) {
			return APIResult.created(goods);
		}else {
			return APIResult.notFound("对象不存在");
		}
	}
	
	/**
	 * 删除修改状态码
	 */
	@DeleteMapping
	public APIResult remove(Integer id) {
		Goods goods = goodsService.remove(id);
		
		if (goods != null) {
			return APIResult.created(goods);
		} else {
			return APIResult.notFound("对象不存在");
		}
	}
	
	@GetMapping
	public APIResult get(Integer page, Integer limit, Goods goods) {
		PageInfo pageInfo = goodsService.getByPage(page,limit,goods);
		
		if (pageInfo == null || pageInfo.getTotal() == 0) {
			return APIResult.notFound("未查询到任何数据");
		} else {
			return APIResult.ok(pageInfo);
		}
	}
}
